package com.zsix.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.zsix.common.common.page.BaseController;
import com.zsix.common.common.page.PageDataTable;
import com.zsix.common.dto.controllerParams.DeploymentInfo;
import com.zsix.common.dto.controllerParams.ProcessDefinitionParams;
import com.zsix.common.dto.controllerParams.TaskDefinitionParams;
import com.zsix.common.entity.process.FlowEntity;
import com.zsix.common.entity.process.FlowSort;
import com.zsix.common.util.ResultRes;
import com.zsix.framework.aspectj.annotation.OperLog;
import com.zsix.framework.aspectj.operate.LogConst;
import com.zsix.system.service.IProcessDefinitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/deployment")
public class ProcessDefinitionController extends BaseController {
    @Autowired
    private IProcessDefinitionService processDefinitionService;

    /**
     * 获取流程分类
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程分类列表")
    @GetMapping("/getFlowSort")
    public PageDataTable getFlowSort(){
        startPage();
        List<FlowSort> list=processDefinitionService.getFlowSort();
        return getDataTable(list);
    }
    /**
     * 获取流程分类树型结构数据
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程分类树形结构数据")
    @GetMapping("/getFlowSortTree")
    public ResultRes getFlowSortTree(){
        try {
            return ResultRes.success(processDefinitionService.getFlowSortTree());
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    /**
     * 获取流程分类树型结构数据
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程分类树形结构数据")
    @GetMapping("/getFlowTree")
    public ResultRes getFlowTree(){
        try {
            return ResultRes.success(processDefinitionService.getFlowTree());
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    /**
    获取流程列表
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程列表")
    @PostMapping("/getDeployment")
    public ResultRes getDeployment(@RequestBody DeploymentInfo deploymentInfo){
        try {
            return ResultRes.success(processDefinitionService.getDeployment(deploymentInfo));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 流程发布
     * @param param
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.INSERT , operDesc = "流程发布")
    @PostMapping(value = "/deployWithBPMNJS")
    public ResultRes deployWithBPMNJS(@RequestBody JSONObject param){
        String stringBPMNXml=param.get("stringBPMNXml").toString();
        try {
            return ResultRes.success(processDefinitionService.deployWithBPMNJS(stringBPMNXml));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    /**
     * 获取流程定义的xml模版
     * @param response
     * @param deploymentId
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程定义的xml模版")
    @GetMapping(value="/getDeploymentXmlById")
    public void getDeploymentXmlById(HttpServletResponse response,
                                     @RequestParam("deploymentId") String deploymentId){
        processDefinitionService.getDeploymentXmlById(response, deploymentId);
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程属性")
    @GetMapping(value="/processProperties")
    public ResultRes processProperties(@RequestParam("process_key") String process_key){
        return ResultRes.success(processDefinitionService.processProperties(process_key));
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.INSERT , operDesc = "更新流程属性")
    @PostMapping(value = "/saveProcessProperties")
    public ResultRes saveProcessProperties(@RequestBody ProcessDefinitionParams processDefinitionParams){
        return ResultRes.success(processDefinitionService.saveProcessProperties(processDefinitionParams));
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取节点属性")
    @GetMapping(value="/userTaskProperties")
    public ResultRes userTaskProperties(@RequestParam("task_def_key") String task_def_key){
        return ResultRes.success(processDefinitionService.userTaskProperties(task_def_key));
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.INSERT , operDesc = "更新节点属性")
    @PostMapping(value = "/saveUserTaskProperties")
    public ResultRes saveUserTaskProperties(@RequestBody TaskDefinitionParams taskDefinitionParams){
        return ResultRes.success(processDefinitionService.saveUserTaskProperties(taskDefinitionParams));
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程发起列表")
    @GetMapping(value="/getSatrtPageData")
    public ResultRes getSatrtPageData(){
        try {
            return ResultRes.success(processDefinitionService.getSatrtPageData());
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程发起列表")
    @GetMapping(value="/commProceList")
    public PageDataTable commProceList(){
        startPage();
        List<FlowEntity> list=processDefinitionService.commProceList();
        return getDataTable(list);
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取用户任务中设置的参数")
    @GetMapping("/getFormElement")
    public ResultRes getFormElement(HttpServletResponse response,
                                    @RequestParam("process_key") String process_key){
        try{
            return ResultRes.success(processDefinitionService.getFormElement(process_key));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
    @OperLog(operModul = "流程定义相关" , operType = LogConst.OTHER , operDesc = "获取流程枚举")
    @GetMapping(value="/getFlowEnum")
    public ResultRes getFlowEnum(){
        try{
            return ResultRes.success(processDefinitionService.getFlowEnum());
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 更新流程配置
     * @param params
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.UPDATE , operDesc = "更新流程配置")
    @PostMapping(value = "/updateDeployment")
    public ResultRes updateDeployment(@RequestBody ProcessDefinitionParams params){
        try{
            return ResultRes.success(processDefinitionService.updateDeployment(params));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 增加流程分类
     * @param flowSort
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.INSERT , operDesc = "增加流程分类")
    @PostMapping(value = "/addFlowSort")
    public ResultRes addFlowSort(@RequestBody FlowSort flowSort){
        try{
            return ResultRes.success(processDefinitionService.addFlowSort(flowSort));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 修改流程分类
     * @param flowSort
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.UPDATE , operDesc = "修改流程分类")
    @PostMapping(value = "/saveFlowSort")
    public ResultRes saveFlowSort(@RequestBody FlowSort flowSort){
        try{
            return ResultRes.success(processDefinitionService.saveFlowSort(flowSort));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 删除流程分类
     * @param flowSort
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.DELETE , operDesc = "删除流程分类")
    @PostMapping(value = "/deleteFlowSort")
    public ResultRes deleteFlowSort(@RequestBody FlowSort flowSort){
        try{
            return ResultRes.success(processDefinitionService.deleteFlowSort(flowSort));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }

    /**
     * 删除流程定义
     * @param deploymentId
     * @return
     */
    @OperLog(operModul = "流程定义相关" , operType = LogConst.DELETE , operDesc = "删除流程定义")
    @GetMapping("/delDefinition")
    public ResultRes delDefinition(@RequestParam("depID") String deploymentId){
        try{
            return ResultRes.success(processDefinitionService.deleteDefinition(deploymentId));
        }
        catch (Exception ex){
            return ResultRes.error(ex.getMessage());
        }
    }
}
